/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.Utilities
*        文件名:             ResourceManager.cs
*        创建系统时间:       2012/11/2 19:07:08
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.Utilities
{
    using System.Collections;
    using System.Collections.Generic;
    using System.IO;

    public class ResourceManager
    {
        private string FolderPath = string.Empty;
        private static volatile ResourceManager instance = null;
        public SortedList<string, Resources> LanguageResources = new SortedList<string, Resources>();
        private static object locker = new object();

        public string Get(string language, string key)
        {
            if (!this.LanguageResources.ContainsKey(language))
            {
                return string.Empty;
            }
            return this.LanguageResources[language].Get(key);
        }

        public Hashtable GetLanguages()
        {
            Hashtable hashtable = new Hashtable();
            IEnumerator<KeyValuePair<string, Resources>> enumerator = this.LanguageResources.GetEnumerator();
            while (enumerator.MoveNext())
            {
                KeyValuePair<string, Resources> current = enumerator.Current;
                KeyValuePair<string, Resources> pair2 = enumerator.Current;
                hashtable.Add(current.Key, pair2.Value.displayName);
            }
            return hashtable;
        }

        public Hashtable GetLanguages(string path)
        {
            Hashtable hashtable = new Hashtable();
            DirectoryInfo info = new DirectoryInfo(path);
            if (info.Exists)
            {
                FileInfo[] files = info.GetFiles();
                for (int i = 0; i < files.Length; i++)
                {
                    Resources resources = ResourcesSerializer.DeSerialize(files[i].FullName);
                    hashtable.Add(resources.language, resources.displayName);
                }
            }
            return hashtable;
        }

        public Resources GetResources(string filePath)
        {
            Resources resources = new Resources();
            if (File.Exists(filePath))
            {
                resources = ResourcesSerializer.DeSerialize(filePath);
                resources.createIndex();
            }
            return resources;
        }

        public void Init(string filePath)
        {
            this.FolderPath = filePath;
            DirectoryInfo info = new DirectoryInfo(filePath);
            this.LanguageResources.Clear();
            if (info.Exists)
            {
                FileInfo[] files = info.GetFiles();
                for (int i = 0; i < files.Length; i++)
                {
                    Resources resources = ResourcesSerializer.DeSerialize(files[i].FullName);
                    resources.createIndex();
                    this.LanguageResources.Add(resources.language, resources);
                }
            }
        }

        public void Serialize(Resources resources, string filePath)
        {
            ResourcesSerializer.Serialize(filePath, resources);
        }

        public static ResourceManager Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (locker)
                    {
                        if (instance == null)
                        {
                            instance = new ResourceManager();
                        }
                    }
                }
                return instance;
            }
        }
    }
}